<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="提成名字" prop="commissionTypeName">
      <el-input v-model="dataForm.commissionTypeName" placeholder="提成名字"></el-input>
    </el-form-item>
    <el-form-item label="提成百分比" prop="percentage">
      <el-input-number :min="1" :max="100" v-model="dataForm.percentage" placeholder="提成百分比"></el-input-number>
    </el-form-item>
    <el-form-item label="状态" prop="status">
			<el-radio v-model="dataForm.status" label="1">启用</el-radio>
			 <el-radio v-model="dataForm.status" label="2">停用</el-radio>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
				disabledSubmit:false,
        dataForm: {
          id: 0,
          commissionTypeName: '',
          sellerId: '',
          consumeMoney: '',
          commissionMoney: '',
          status: '1',
          percentage: '',
          remark: ''
        },
        dataRule: {
          commissionTypeName: [
            { required: true, message: '提成名字不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '租户不能为空', trigger: 'blur' }
          ],
          consumeMoney: [
            { required: true, message: '消费金额不能为空', trigger: 'blur' }
          ],
          commissionMoney: [
            { required: true, message: '提成金额不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          percentage: [
            { required: true, message: '提成百分比不能为空', trigger: 'blur' }
          ],
          // remark: [
          //   { required: true, message: '备注不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/member/tstorestaffcommissiontype/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.commissionTypeName = data.tStoreStaffCommissionType.commissionTypeName
                this.dataForm.sellerId = data.tStoreStaffCommissionType.sellerId
                this.dataForm.consumeMoney = data.tStoreStaffCommissionType.consumeMoney
                this.dataForm.commissionMoney = data.tStoreStaffCommissionType.commissionMoney
                this.dataForm.status = data.tStoreStaffCommissionType.status.toString()
                this.dataForm.percentage = data.tStoreStaffCommissionType.percentage
                this.dataForm.remark = data.tStoreStaffCommissionType.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/member/tstorestaffcommissiontype/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'commissionTypeName': this.dataForm.commissionTypeName,
                'sellerId': this.dataForm.sellerId,
                'consumeMoney': this.dataForm.consumeMoney,
                'commissionMoney': this.dataForm.commissionMoney,
                'status': this.dataForm.status,
                'percentage': this.dataForm.percentage,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
										this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.disabledSubmit = false
              }
            })
          }else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
